package com.edu.user.controller;

import com.edu.user.domain.dto.DiseaseDTO;
import com.edu.user.domain.po.Disease;
import com.edu.user.service.DiseaseService;
import com.edu.user.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/diseases")
public class DiseaseController {

    private final DiseaseService diseaseService;

    @Autowired
    public DiseaseController(DiseaseService diseaseService) {
        this.diseaseService = diseaseService;
    }

    @GetMapping("/all")
    public Result getAllDiseaseIdsAndNames() {
        log.info("getAllDiseaseIdsAndNames: 尝试获取所有疾病的ID和名称");
        List<DiseaseDTO> diseases = diseaseService.getAllDiseaseIdsAndNames();
        log.info("getAllDiseaseIdsAndNames: 成功获取所有疾病的ID和名称，共计 {} 条记录", diseases.size());
        return Result.success(diseases);
    }

    @GetMapping("/{diseaseId}")
    public Result getDiseaseById(@PathVariable Long diseaseId) {
        log.info("getDiseaseById: 尝试获取疾病ID为 {} 的信息", diseaseId);
        Disease disease = diseaseService.getDiseaseById(diseaseId);
        if (disease == null) {
            log.warn("getDiseaseById: 疾病ID为 {} 的信息不存在", diseaseId);
            return Result.error("疾病不存在");
        }
        log.info("getDiseaseById: 成功获取疾病ID为 {} 的信息", diseaseId);
        return Result.success(disease);
    }
}
